package com.miyuan.view.machine;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;
import android.widget.Toast;

import com.miyuan.R;
import com.miyuan.base.BaseFragment;
import com.miyuan.bean.ResultBean;
import com.miyuan.bean.request.MachineGoodsParam;
import com.miyuan.bean.respone.MachineDetailsBean;
import com.miyuan.bean.respone.MachineGoodsBean;
import com.miyuan.net.NetService;
import com.miyuan.net.RetrofitClient;
import com.miyuan.view.home.MachineListFragment;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 售货机信息详情
 */

public class MachineInfosFragment extends BaseFragment {
    @BindView(R.id.machine_code)
    TextView machineCode;
    @BindView(R.id.machine_location)
    TextView machineLocation;
    @BindView(R.id.phone_num)
    TextView phoneNum;
    @BindView(R.id.driver_code)
    TextView driverCode;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.control_code)
    TextView controlCode;

    MachineDetailsBean machineDetailsBean;

    @OnClick(R.id.update_content)
    void update() {
        if (machineDetailsBean != null) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            Bundle bundle = getArguments();
            bundle.putSerializable("machineDetailsBean", machineDetailsBean);
            ModifyDetialsFragment modifyDetialsFragment = ModifyDetialsFragment.newInstance(bundle);
            transaction.replace(R.id.root_fragment, modifyDetialsFragment);
            transaction.addToBackStack("modifyDetialsFragment");
            transaction.commit();
        } else {
            Toast.makeText(getActivity(), "未获取到售货机详情", Toast.LENGTH_SHORT).show();
        }
    }

    public static MachineInfosFragment newInstance(Bundle args) {
        MachineInfosFragment machineInfosFragment = new MachineInfosFragment();
        machineInfosFragment.setArguments(args);
        return machineInfosFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setFragmentLayout(R.layout.fragment_machine_infos);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String vendorId = getArguments().getString("vendorId");
        if (vendorId != null) {
            RetrofitClient.getInstance()
                    .create(NetService.class)
                    .getMachineDetials(vendorId)
                    .enqueue(new Callback<ResultBean<MachineDetailsBean>>() {

                        @Override
                        public void onResponse(Call<ResultBean<MachineDetailsBean>> call, Response<ResultBean<MachineDetailsBean>> response) {
                            ResultBean<MachineDetailsBean> resultBean = response.body();
                            if (resultBean.getData() != null) {
                                machineDetailsBean = resultBean.getData();
                                machineCode.setText(machineDetailsBean.getNumber());
                                machineLocation.setText(machineDetailsBean.getPointName());
                                phoneNum.setText(machineDetailsBean.getPhone());
                                driverCode.setText(machineDetailsBean.getJobNumber());
                                controlCode.setText(machineDetailsBean.getControlNumber());
                                time.setText(machineDetailsBean.getLaunchDate());
                            } else {
                                Toast.makeText(getActivity(), resultBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResultBean<MachineDetailsBean>> call, Throwable t) {

                        }
                    });
        }
    }

    @OnClick(R.id.back_layout)
    public void onBack(){
        getFragmentManager().popBackStack();
    }
}
